﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;

namespace LiteratureManage.Web.Controllers
{
    using LiteratureManage.Domain;
    using LiteratureManage.Domain.Models;
    using LiteratureManage.Domain.Services;
    using LiteratureManage.Web.ViewModels;

    public partial class LiteratureController : BaseController
    {
        private readonly ILiteratureService _service;

        public LiteratureController(ILiteratureService service)
        {
            this._service = service;
        }

        public virtual ActionResult Index()
        {
            return View();
        }

        public virtual ActionResult Create()
        {
            return View(MVC.Literature.Views.Edit);
        }

        [HttpPost]
        public virtual ActionResult Create(Literature model)
        {
            var id = 0;
            return CheckValidCall(() =>
            {
                id = this._service.Create(model);
            })
            .Valid(() => this.Success("操作成功", new{ url = Url.Action(MVC.Literature.Edit(id))}))
            .Invalid(() => this.Failture("操作失败"));
        }

        public virtual ActionResult Edit(int id)
        {
            var model = this._service.Get(id);
            return View(model);
        }

        [HttpPost]
        public virtual ActionResult Edit(Literature model)
        {
            return CheckValidCall(() => 
            {
                this._service.Update(model);
            })
            .Valid(() => this.Success("操作成功"))
            .Invalid(() => this.Failture("操作失败"));
        }

        public virtual ActionResult List([DefaultValue(1)]int page, LiteratureSearchViewModel model)
        {
            //var pagingResult = this._service.GetAll(page, 10);
            var pagingResult = this._service.Search(page, 10, model.CreateSearchableTerm());
            return View(pagingResult);
        }

        public virtual ActionResult Search([DefaultValue(1)]int page, LiteratureSearchViewModel model)
        {
            var pagingResult = this._service.Search(page, 10, model.CreateSearchableTerm());
            return View(pagingResult);
        }

        public virtual ActionResult ListAuthors(Literature model)
        {
            return View(model);
        }

        //[HttpPost]
        public virtual ActionResult AjaxListAuthors(int literatureId)
        {
            var literature = this._service.Get(literatureId);
            //var model = (from a in literature.Authors
            //             select new
            //             {
            //                 Id = a.Id,
            //                 Name = a.Name,
            //                 Gender = a.Gender,
            //                 Org = a.Org,
            //                 Phone = a.Phone,
            //                 Mobile = a.Mobile,
            //                 Address = a.Address,
            //                 Email = a.Email
            //             }).ToList();

            //return Json(model, JsonRequestBehavior.AllowGet);
            return View(literature);
        }

        public virtual ActionResult CreateAuthor(int literatureId)
        {
            return View(MVC.Literature.Views.EditAuthor);
        }

        [HttpPost]
        public virtual ActionResult CreateAuthor(int literatureId, Author model)
        {
            return CheckValidCall(() =>
            {
                this._service.CreateAuthor(literatureId, model);
            })
            .Valid(() => this.Success("操作成功", new { url = Url.Action(MVC.Literature.Edit(literatureId)) }))
            .Invalid(() => this.Failture("操作失败"));
        }

        public virtual ActionResult EditAuthor(int literatureId, int id)
        {
            var literature = this._service.Get(literatureId);
            var model = (from a in literature.Authors where a.Id == id select a).SingleOrDefault();
            return View(model);
        }

        [HttpPost]
        public virtual ActionResult EditAuthor(int literatureId, Author model)
        {
            return CheckValidCall(() =>
            {
                this._service.UpdateAuthor(literatureId, model);
            })
            .Valid(() => this.Success("操作成功", new { url = Url.Action(MVC.Literature.Edit(literatureId)) }))
            .Invalid(() => this.Failture("操作失败"));
        }

        [HttpPost]
        public virtual ActionResult DeleteAuthor(int literatureId, int id)
        {
            this._service.DeleteAuthor(literatureId, id);
            return this.Success("操作成功", new { url = Url.Action(MVC.Literature.Edit(literatureId)) });
        }
    }
}
